package com.hulk.other.springStudy.aop;

import java.util.HashMap;
import java.util.Map;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hulk.other.springStudy.service.PersionService2;


@Component
@Aspect
public class TheInterceptor {

	@Autowired
	private PersionService2 persionService2;
	
//	@Pointcut("execution (* com.hulk.springStudy.service.*Service*.*(..))")
	@Pointcut("execution (* com.hulk.other.springStudy.service.PersionServiceImpl2.savePersion(..))")
	// 声明一个切入点(第一个*后要留一个空格)
	
	public void pointCut() {
	}

	@Before("pointCut()")
	public void before(JoinPoint joinPoint) {
		// 如果需要这里可以取出参数进行处理
		// Object[] args = joinPoint.getArgs();
		System.out.println("执行方法前");
	}

	@After("pointCut()")
	public void after(JoinPoint joinPoint) {
		System.out.println("执行方法后");
	}

	@AfterReturning(pointcut = "pointCut()", returning = "returnVal")
	public void afterReturning(JoinPoint joinPoint, Object returnVal) {
		Map m = new HashMap();
		m.put("aa", "aa");
		persionService2.delPersion(m);
		System.out.println("返回结果后"
				+ returnVal);
	}

	@Around("pointCut()")
	public void around(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println("around start..");
		try {
			pjp.proceed();
		} catch (Throwable ex) {
			System.out.println("发生异常......");
			throw ex;
		}
		System.out.println("around end");
	}

	@AfterThrowing(pointcut = "pointCut()", throwing = "error")
	public void afterThrowing(JoinPoint jp, Throwable error) {
		System.out.println("异常后" + error);
	}
}
